<template>
  <div>
    <breadcrumb-nav menu1="客户管理" menu2="客户列表"></breadcrumb-nav>

    <el-card>
      <!-- 搜索区 -->
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入姓名"
            clearable
            @keyup.enter.native="doQuery"
          />
        </el-form-item>
        <el-form-item label="公司" prop="company">
          <el-input
            v-model="queryParams.company"
            placeholder="请输入公司"
            clearable
            @keyup.enter.native="doQuery"
          />
        </el-form-item>
        <el-form-item label="国家" prop="country">
          <el-select placeholder="请输入或选择" v-model="queryParams.country" clearable filterable>
            <el-option v-for="item in countryList" :value="item" :label="item" :key="item">{{item}}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="queryParams.email"
            placeholder="请输入邮箱"
            clearable
            @keyup.enter.native="doQuery"
          />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input
            v-model="queryParams.mobile"
            placeholder="请输入手机"
            clearable
            @keyup.enter.native="doQuery"
          />
        </el-form-item>
        <el-form-item label="重要程度" prop="star">
          <el-select v-model="queryParams.star" clearable placeholder="请选择">
            <el-option
              v-for="item in starOptions"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="doQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格区 -->
      <el-table :data="customerList" stripe @sort-change="handleSortChange">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="姓名" prop="name"></el-table-column>
        <el-table-column label="公司" prop="company"></el-table-column>
        <el-table-column label="国家" prop="country" width="100px"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="重要程度" prop="star" sortable="custom" align="center" width="100px"></el-table-column>
        <el-table-column label="最后跟单日期" prop="lastFollow" sortable="custom" width="140px"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUser(scope.row.id)"></el-button>
            <el-tooltip content="查看详情" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-tickets" size="mini" @click="gotoRecordPage(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <div class="card-footer">
        <el-button type="primary" @click="showAddDialog">添加客户</el-button>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="queryParams.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 添加/编辑客户对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="50%"
      @close="dialogClosed">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="客户名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="公司" prop="company">
          <el-input v-model="form.company"></el-input>
        </el-form-item>
        <el-form-item label="地区/国家">
          <el-input v-model="form.country"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="Skype">
          <el-input v-model="form.skype"></el-input>
        </el-form-item>
        <el-form-item label="Whatsapp">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="重要程度">
          <el-select v-model="form.star">
            <el-option
              v-for="item in starOptions"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryParams: {
        name: '',
        company: '',
        country: '',
        email: '',
        mobile: '',
        star: '',
        page: 1,
        limit: 10,
        orderColumn: '',
        order: ''
      },
      starOptions: [0, 1, 2, 3, 4, 5],
      customerList: [],
      total: 0,
      dialogVisible: false, // 新增/编辑窗口是否可见
      editing: false, // 编辑or新增
      title: '添加客户', // 新增/编辑窗口的标题
      form: {}, // 新增/编辑表单
      rules: {
        name: [
          { required: true, message: '请输入客户名', trigger: 'blur' }
        ],
        company: [
          { required: true, message: '请输入公司', trigger: 'blur' }
        ],
        email: [
          { type: 'email', message: '请输入合法的邮箱账号', trigger: 'blur' }
        ]
      },
      // 要被分配角色的用户信息
      userinfo: {},
      setRoleDialogVisiable: false,
      roleList: [],
      selectedRoleId: '',
      countryList: []
    }
  },

  methods: {
    getList() {
      this.$http.get('customers', { params: this.queryParams })
        .then(resp => {
          this.customerList = resp.data.records
          this.total = resp.data.total
        })
    },
    doQuery() {
      this.$http.get('customers', { params: this.queryParams })
      this.queryParams.page = 1
      this.getList()
    },
    resetQuery() {
      this.queryParams.name = ''
      this.queryParams.company = ''
      this.queryParams.country = ''
      this.queryParams.email = ''
      this.queryParams.mobile = ''
      this.queryParams.star = ''
    },
    handleSizeChange(val) {
      this.queryParams.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.page = val
      this.getList()
    },
    handleSortChange(val) {
      this.queryParams.orderColumn = val.prop
      this.queryParams.order = val.order
      this.getList()
    },
    dialogClosed() {
      this.$refs.form.resetFields()
    },
    showAddDialog() {
      this.form = {
        name: '',
        company: '',
        country: '',
        email: '',
        mobile: '',
        skype: '',
        whatsapp: '',
        star: 0
      }
      this.title = '添加用户'
      this.editing = false
      this.dialogVisible = true
    },
    doSave() {
      if (this.editing) {
        this.editCustomer()
      } else {
        this.addCustomer()
      }
    },
    addCustomer() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.$http.post('customers', this.form).then(resp => {
          this.$message.success('添加客户成功！')
          this.dialogVisible = false
          this.getList()
        })
      })
    },
    editCustomer() {

    },
    gotoRecordPage(customerInfo) {
      this.$router.push('/customers/' + customerInfo.id)
    },
    getCountryList() {
      this.$http.get('customers/country')
        .then(resp => {
          if (resp.code === 200) {
            this.countryList = resp.data
            console.log(this.countryList)
          }
        })
    }
  },

  created() {
    this.doQuery()
    this.getCountryList()
  }
}
</script>

<style>
</style>
